<?php
namespace Admin\Controller;
use User\Api\UserApi as UserApi;

/**
 * 后台订单控制器
 * @author keg
 */
class OrderController extends AdminController {

    /**
     * 订单列表
     * @author keg
     */
    public function index(){
        $username = I('username', '');
        $map = array();
        $list = array(0,1,2);

        $this->assign('list', $list);
        $this->meta_title = '订单列表';
        $this->display();
    }

    
    /**
     * 添加
     * @author keg
     */
    public function add(){
        if(IS_POST){
            $title = I('post.title', '');
            $pid = I('post.pid', 0);
            $sort = I('post.sort', 10);
            $value = I('post.value', '');
            $add_time = $update_time = NOW_TIME;

            //输入信息验证
            !$title && $this->error('规格名称不能为空');

            //品牌重复性检测
            //::TODO

            //处理结果
            $data['title'] = $title;
            $data['pid'] = $pid;
            $data['sort'] = $sort;
            $data['value'] = $value;
            $data['add_time'] = $add_time;
            $data['update_time'] = $update_time;

            //新增操作
            if(M('GoodsSpecs')->add($data)){
                $this->success('新增成功', U('GoodsSpecs/index', array('pid'=>$pid)));
            }else{
                $this->error('新增失败');
            }
            
        }

        $pid = I('pid', 0);
        $info = M('GoodsCategory')->find($pid);


        $this->assign('info', $info);
        $this->meta_title = '新增分类属性';
        $this->display();
    }

    //编辑
    public function edit(){
        $id = I('id', 0);
        !$id && $this->error('修改信息获取失败');

        if(IS_POST){
            $title = I('post.title', '');
            $pid = I('post.pid', 0);
            $sort = I('post.sort', 10);
            $value = I('post.value', '');
            $add_time = $update_time = NOW_TIME;

            //输入信息验证
            !$title && $this->error('规格名称不能为空');

            //品牌重复性检测
            //::TODO

            //处理结果
            $data['id'] = $id;
            $data['title'] = $title;
            $data['pid'] = $pid;
            $data['sort'] = $sort;
            $data['value'] = $value;
            $data['update_time'] = $update_time;

            //新增操作
            if(M('GoodsSpecs')->save($data)){
                $this->success('修改成功', U('GoodsSpecs/index', array('pid'=>$pid)));
            }else{
                $this->error('修改失败');
            }
            
        }

        //修改信息获取
        $info = M('GoodsSpecs')->find($id);
        $pname = M('GoodsCategory')->where('id=' . $info['pid'])->getField('title');
        !$pname && $pname = '顶级分类';

        $this->assign('pname', $pname);
        $this->assign('info', $info);
        $this->display();
    }

    //删除操作
    public function del(){
        $ids = I('id', 0);
        $pid = I('get.pid', 0);
        //id验证
        !$ids && $this->error('请选择需要删除的数据');
        //需要删除的id组合成数组
        $id = array_unique((array)$ids);
        $map['id'] = array('in',$id);

        //删除操作
        if(M('GoodsSpecs')->where($map)->delete()){
            $this->success('删除成功', U('GoodsSpecs/index', array('pid'=>$pid)));
        }else{
            $this->error('删除失败');
        }
    }


    //显示隐藏问题
    public function status(){
        $ids = I('id', 0);
        $status = I('get.status',0);
        $pid = I('pid', '');

        //id验证
        !$ids && $this->error('请选择要操作的数据');
        $id = array_unique((array)$ids);
        $map['id'] = array('in',$id);
        //操作
        if(M('GoodsSpecs')->where($map)->setField('status', $status)){
            $this->success('操作成功', U('GoodsSpecs/index', array('pid'=>$pid)));
        }else{
            $this->error('操作失败');
        }
    }
    
}
